﻿    using BCCommon;
using BCData.Common.Banner;
using BCDto.Common.Banner;
using BCEntity.Common.Banner;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.Common.Banner
{
    /// <summary>
    /// Banner服务
    /// </summary>
    public class BannerService : IBannerService
    {
        private readonly IBannerData bannerData;
        private readonly IBannerTagcodeData bannerTagcodeData;
        private readonly IRedisService redisService;
        private readonly string key = string.Format("{0}", EntityNameConst.BannerEntity);
        public BannerService(IRedisService redisService,IBannerData bannerData, IBannerTagcodeData bannerTagcodeData)
        {
            this.redisService = redisService;
            this.bannerData = bannerData;
            this.bannerTagcodeData = bannerTagcodeData;
        }

        public BannerDto Add(BannerRequestDto bannerRequestDto)
        {
            if (this.bannerData.CheckBannerExists(bannerRequestDto.Name))
            {
                throw new ArgumentException("当前Banner信息已存在。");
            }
            if (!this.bannerTagcodeData.CheckBannerTagcodeExists(bannerRequestDto.BannerTagcode))
            {
                throw new ArgumentException("当前Banner分类信息不存在。");
            }
            return this.bannerData.Add(bannerRequestDto.As<BannerEntity>()).As<BannerDto>();
        }

        public void Delete(long bannerId)
        {
            if (!this.Exists(bannerId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            this.bannerData.Delete(bannerId);
            string cacheKey = this.redisService.GetKey(key, bannerId);
            this.redisService.DeleteAsync(cacheKey);
        }

        public IEnumerable<BannerDto> Get(string bannerTagcode)
        {
            return this.bannerData.Get(bannerTagcode).As<IEnumerable<BannerDto>>();
        }

        public BannerDto GetEntity(long bannerId)
        {
            string cacheKey = this.redisService.GetKey(key, bannerId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.bannerData.GetEntity(bannerId);
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<BannerDto>();
        }

        public PageableList<BannerDto> GetPageableList(string name, string bannerTagcode, bool? ascending, int pageIndex = 1, int pageSize = 20)
        {
            var list = this.bannerData.GetPageableList(name, bannerTagcode, out int dataCount, ascending, pageIndex, pageSize);

            return new PageableList<BannerDto>
            {
                Count = dataCount,
                Items = list.As<IEnumerable<BannerDto>>()
            };
        }

        public BannerDto Update(long bannerId, BannerRequestDto bannerRequestDto)
        {
            if (!this.bannerData.CheckBannerExists(bannerId))
            {
                throw new ArgumentException("此Banner信息不存在。");
            }
            if (!this.bannerTagcodeData.CheckBannerTagcodeExists(bannerRequestDto.BannerTagcode))
            {
                throw new ArgumentException("当前Banner分类信息不存在。");
            }
            BannerEntity entity = bannerRequestDto.As<BannerEntity>();
            entity.BannerId = bannerId;
            return this.bannerData.Update(entity).As<BannerDto>();
        }
        #region 检查Banner是否存在
        private bool Exists(long bannerId)
        {
            return this.bannerData.Exists(bannerId).GetAwaiter().GetResult();
        }
        #endregion

        #region 启用Banner
        public bool Enabled(long bannerId)
        {
            if (!this.Exists(bannerId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.bannerData.Enabled(bannerId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, bannerId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 禁用Banner
        public bool Disabled(long bannerId)
        {
            if (!this.Exists(bannerId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.bannerData.Disabled(bannerId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, bannerId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion
    }
}
